package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
	"time"
	"yklx4/cache"
	"yklx4/models"
	"yklx4/pkg"
)

type WWW struct {
	beego.Controller
}
type Msg struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (w *WWW) Deng() {
	username := w.GetString("username")
	password := w.GetString("password")
	if username == "" || password == "" {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "密码或者用户名不为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	var err error
	var users models.Users
	err = users.DataAdd(username, &users)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户名错误",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	cacheKey := "redisId" + username
	cacheCount, _ := cache.Rdb.Get(cache.Ctx, cacheKey).Int()
	if cacheCount >= 3 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "密码错误3次,请稍后再试",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	if users.Password != pkg.Md5str1(password) {
		cache.Rdb.Incr(cache.Ctx, cacheKey)
		if cacheCount == 2 {
			cache.Rdb.Set(cache.Ctx, cacheKey, 3, time.Hour*2)
			w.Data["json"] = Msg{
				Code: 0,
				Msg:  "密码错误三次,请稍后再试",
				Data: nil,
			}
			w.ServeJSON()
			return
		}
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "密码错误",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "登录成功",
		Data: users,
	}
	w.ServeJSON()
}

func (w *WWW) QuanrolegxbAdd() {
	qid, _ := w.GetInt("qid", 0)
	rid, _ := w.GetInt("rid", 0)
	if qid == 0 || rid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "权限ID和角色ID不为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	data := models.Quanrolegxb{
		Qid: qid,
		Rid: rid,
	}
	_, err := data.AddData(&data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "添加成功",
		Data: data,
	}
	w.ServeJSON()
}

func (w *WWW) Quanrolegxblist() {
	uid, _ := w.GetInt("uid", 0)
	if uid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户ID不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	var rols models.Role
	list, err := rols.List(uid)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: list,
	}
	w.ServeJSON()
	return
}
func (w *WWW) QuanrolegxbLists() {
	rid, _ := w.GetInt("rid", 0)
	if rid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户ID不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	var rols models.Quan
	list, err := rols.QuanrolegxbListss(rid)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: list,
	}
	w.ServeJSON()
	return
}
func (w *WWW) CompanssAdd() {
	comname := w.GetString("comname")
	if comname == "" {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "物流名称不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	data := models.Companss{
		Comname: comname,
	}

	_, err := data.Addiseds(&data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "添加成功",
		Data: data,
	}
	w.ServeJSON()
	return

}
func (w *WWW) OrdersAdd() {
	ordername := w.GetString("ordername")
	tid, _ := w.GetInt("tid", 0)
	cid, _ := w.GetInt("cid", 0)
	if ordername == "" || tid == 0 || cid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "快递单号,快递类型ID,物流名称ID不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	data := models.Orders{
		Ordername: ordername,
		Tid:       tid,
		Cid:       cid,
	}

	_, err := data.OrdersAdds(&data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "添加成功",
		Data: data,
	}
	w.ServeJSON()
	return

}
func (w *WWW) Listssss() {
	page, _ := w.GetInt("page", 1)
	size, _ := w.GetInt("size", 3)
	offset := (page - 1) * size

	var err error
	var count int64
	var list []map[string]interface{}
	listKey := "listID" + strconv.Itoa(page)
	listcount, _ := cache.Rdb.Get(cache.Ctx, listKey).Result()
	if listcount == "" {
		err = models.DB.Table("orders").
			Joins("LEFT JOIN compansses ON compansses.id = orders.cid").
			Joins("LEFT JOIN tyeps ON tyeps.id = orders.tid ").
			Select("orders.id,compansses.comname,tyeps.tyepname,orders.ordername").
			Offset(offset).Limit(size).Scan(&list).Error
		cahceJson, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, listKey, cahceJson, time.Hour*2)
	} else {
		json.Unmarshal([]byte(listcount), &list)
	}

	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	pageData := make(map[string]interface{})
	pageData["count"] = count
	pageData["list"] = list
	w.Data["json"] = Msg{
		Code: 20,
		Msg:  "查询成功",
		Data: pageData,
	}
	w.ServeJSON()
	return
}
func (w *WWW) OrdersListss() {
	id, _ := w.GetInt("id", 0)
	if id == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "ID不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	var err error
	var list models.Orders

	key := "id" + strconv.Itoa(id)
	cacheCoun, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if cacheCoun == "" {
		list, err = list.Orderslist(id)
		if err != nil {
			w.Data["json"] = Msg{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			w.ServeJSON()
			return
		}
		datajson, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, key, datajson, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheCoun), &list)
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: list,
	}
	w.ServeJSON()
	return

}
